//
//  Label.swift
//  iOSProjectExtensionSwift
//
//  Created by Apple on 2020/1/11.
//  Copyright © 2020 lidianchao. All rights reserved.
//

import UIKit
extension UILabel {
    ///  创建label
    /// - Parameters:
    ///   - frame: 位置
    ///   - title: 内容
    ///   - textColor:  字体颜色
    ///   - isCenter:  是否居中
    ///   - font: 字体
    ///   - haveUnderLine: 是否有下划线
    ///   - board: 是否加粗
    static func createLabel(frame:CGRect, title:String, textColor:UIColor, isCenter:Bool, font:CGFloat, board:Bool)->UILabel {
        let label = UILabel.init(frame: frame);
        label.text = title;
        label.textColor = textColor;
        label.font = UIFont.init(name: board ? kFontNameBold() : kFontNameLight(), size: font);
        label.textAlignment = isCenter ? NSTextAlignment.center : NSTextAlignment.left;
        label.adjustsFontSizeToFitWidth = true;
        return label;
    }
    ///  创建横线
    /// - Parameter frame:  位置
    static func createLineLabelWithFrame(frame:CGRect) ->UILabel {
        let label1 = UILabel.init(frame: frame);
        label1.backgroundColor = kLineColor;
        return label1;
    }
    ///  得到label的宽度
    /// - Parameters:
    ///   - text: 文字
    ///   - font: 字体
    static func getTextWidthWithString(text:String, font:CGFloat) ->CGFloat {
        return getTextWidthWithString(text: text, font: font, bord: false);
    }
    static func getTextWidthWithString(text:String, font:CGFloat, bord:Bool) ->CGFloat {
        
        let label = UILabel(frame: CGRect(x: 15, y: 0, width: DeviceWidth-30, height: 60));
        
        label.lineBreakMode = NSLineBreakMode.byWordWrapping;
        label.backgroundColor = UIColor.clear;
        /*设置label的frame值*/
        let paragraphStyle = NSMutableParagraphStyle();
        paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping;
        label.numberOfLines = 0;
        label.font = UIFont(name: bord ? kFontNameBold() : kFontNameLight(), size: labelMiddleFont);
        
        let attributedString = NSMutableAttributedString(string: text);
        paragraphStyle.lineSpacing = 5;
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange.init(location: 0, length: text.count));
        label.attributedText = attributedString;
        //调节高度
        let size = CGSize(width: DeviceWidth-30, height: 20);
        let labelSize = label.sizeThatFits(size);
        return labelSize.width;
    }
    static func setIntroductionTextBody(text:String, font:CGFloat) -> UILabel {
        return setIntroductionTextBody(text: text, font: font, frame: CGRect(x: 15, y: 0, width: DeviceWidth-30, height: 20));
    }
    static func setIntroductionTextBody(text:String, font:CGFloat,frame:CGRect) -> UILabel {
        return setIntroductionTextBody(text: text, font: font, frame: CGRect(x: 15, y: 0, width: DeviceWidth-30, height: 20),textColor: kTextColor);
    }
    static func setIntroductionTextBody(text:String, font:CGFloat,frame:CGRect,textColor:UIColor) -> UILabel {
        return setIntroductionTextBody(text: text, font: font, frame: CGRect(x: 15, y: 0, width: DeviceWidth-30, height: 20),textColor: kTextColor,board: false);
    }
    ///  自适应内容的label
    /// - Parameters:
    ///   - text: 内容
    ///   - font: 字体
    ///   - frame: 位置
    ///   - textColor: 字体颜色
    ///   - board: 是否加粗
    static func setIntroductionTextBody(text:String, font:CGFloat,frame:CGRect,textColor:UIColor, board:Bool) -> UILabel {
        
        let label = UILabel(frame: frame);
        label.lineBreakMode = NSLineBreakMode.byWordWrapping;
        label.backgroundColor = UIColor.clear;
        /*设置label的frame值*/
        let paragraphStyle = NSMutableParagraphStyle();
        paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping;
        label.numberOfLines = 0;
        label.font = UIFont(name: board ? kFontNameBold() : kFontNameLight(), size:font);
        label.textColor = UIColor.white;
        let attributedString = NSMutableAttributedString(string: text);
        paragraphStyle.lineSpacing = 5;
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange.init(location: 0, length: text.count));
        attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value:textColor, range: NSRange.init(location: 0, length: text.count));
        label.attributedText = attributedString;
        
        let array = text.components(separatedBy: "\n");
        
        for value in array {
            let temp :Array = value.components(separatedBy:"：");
            if (!temp.isEmpty && temp.count>1) {
                let location = getLoactionInString(str: text, subStr: value)
                let range1 = NSMakeRange(location, temp[0].count)
                attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value:kTextColor, range: range1);
            }
        }
        label.attributedText = attributedString;
        //调节高度
        let size = CGSize(width: frame.size.width, height: 500000);
        let labelSize = label.sizeThatFits(size);
        label.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: labelSize.height);
        return label;
    }
    /// 创建不同颜色的label
    /// - Parameters:
    ///   - text:  总文本
    ///   - textColor: 主体颜色
    ///   - specialText: 特殊颜色的文本
    ///   - specialColor: 特殊颜色
    ///   - font: 字体
    ///   - center: 是否居中
    ///   - frame: 位置
    static func createLabelWithNormolText(text:String, textColor:UIColor, specialText:String, specialColor:UIColor, font:CGFloat, center:Bool, frame:CGRect) ->UILabel {
        
        let label = createLabel(frame: frame, title: text, textColor: textColor, isCenter: center, font: font, board: false);
        let str = NSMutableAttributedString(string: text);
        str.addAttribute(NSAttributedString.Key.foregroundColor, value:textColor, range: NSMakeRange(0, text.count));

        str.addAttribute(NSAttributedString.Key.foregroundColor, value:specialColor, range: NSMakeRange(getLoactionInString(str: text, subStr: specialText), specialText.count));

        label.attributedText = str;
        
        return label;
    }
    func addUnderLine(label:UILabel) {
        // 下划线
        let label1 = UILabel.createLineLabelWithFrame(frame: CGRect.init(x: 0, y: label.bounds.size.height-2, width: label.bounds.size.width, height: kLineWitdth));
        /*设置label的frame值*/
        let paragraphStyle = NSMutableParagraphStyle.init();
        paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping;
        label.numberOfLines = 0;
        paragraphStyle.lineSpacing = 5;
        let attributedString = NSMutableAttributedString.init(string: label.text!);
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange.init(location: 0, length: label.text!.count));
        label.attributedText = attributedString;
        let size = label.sizeThatFits(CGSize.init(width: DeviceWidth, height: 20));
        label1.frame = CGRect.init(x: -2, y: label.bounds.size.height-2, width: size.width+30, height: kLineWitdth);
        //赋值
        label.addSubview(label1);
    }
    /*********************private******************/
    //获取子字符串在主串的位置
    static private func getLoactionInString(str:String, subStr:String) -> Int {
        let range = str.range(of: subStr)!;
        let location :Int = str.distance(from: str.startIndex, to: range.lowerBound);
        return location;
    }
}
